package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.TrayDetailRecordMapper;
import com.canaan.business.domain.bill.TrayDetailRecord;
import com.canaan.business.service.bill.ITrayDetailRecordService;
import com.canaan.common.annotation.TableName;
/**
 * 托盘明细纪录Service业务层处理
 *
 * @author canaan
 * @date 2025-04-03
 */
@Service
public class TrayDetailRecordServiceImpl implements ITrayDetailRecordService {
    @Resource
    private TrayDetailRecordMapper trayDetailRecordMapper;

    /**
     * 查询托盘明细纪录
     *
     * @param id 托盘明细纪录主键
     * @return 托盘明细纪录
     */
    @Override
    public TrayDetailRecord selectTrayDetailRecordById(Long id) {
        return trayDetailRecordMapper.selectTrayDetailRecordById(id);
    }

    /**
     * 查询托盘明细纪录列表
     *
     * @param trayDetailRecord 托盘明细纪录
     * @return 托盘明细纪录
     */
    @Override
    public List<TrayDetailRecord> selectTrayDetailRecordList(TrayDetailRecord trayDetailRecord) {
        return trayDetailRecordMapper.selectTrayDetailRecordList(trayDetailRecord);
    }

    /**
     * 新增托盘明细纪录
     *
     * @param trayDetailRecord 托盘明细纪录
     * @return 结果
     */
    @Override
    public int insertTrayDetailRecord(TrayDetailRecord trayDetailRecord) {
        trayDetailRecord.setCreateBy(SecurityUtils.getUsername());
        trayDetailRecord.setCreateTime(DateUtils.getNowDate());
        return trayDetailRecordMapper.insertTrayDetailRecord(trayDetailRecord);
    }

    /**
     * 批量新增托盘明细纪录
     *
     * @param trayDetailRecordList 托盘明细纪录列表
     * @return 结果
     */
    @Override
    public int batchInsertTrayDetailRecord(List<TrayDetailRecord> trayDetailRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayDetailRecord trayDetailRecord : trayDetailRecordList){
            trayDetailRecord.setCreateBy(username);
            trayDetailRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<trayDetailRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > trayDetailRecordList.size()) {
                endIndex = trayDetailRecordList.size();
            }
            insertNum = insertNum + trayDetailRecordMapper.batchInsertTrayDetailRecord(trayDetailRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改托盘明细纪录
     *
     * @param trayDetailRecord 托盘明细纪录
     * @return 结果
     */
    @Override
    public int updateTrayDetailRecord(TrayDetailRecord trayDetailRecord) {
        trayDetailRecord.setUpdateBy(SecurityUtils.getUsername());
        trayDetailRecord.setUpdateTime(DateUtils.getNowDate());
        return trayDetailRecordMapper.updateTrayDetailRecord(trayDetailRecord);
    }

    /**
     * 批量修改托盘明细纪录
     *
     * @param trayDetailRecordList 托盘明细纪录列表
     * @return 结果
     */
    @Override
    public int batchUpdateTrayDetailRecord(List<TrayDetailRecord> trayDetailRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayDetailRecord trayDetailRecord : trayDetailRecordList){
            trayDetailRecord.setUpdateBy(username);
            trayDetailRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<trayDetailRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > trayDetailRecordList.size()) {
                endIndex = trayDetailRecordList.size();
            }
            updateNum = updateNum + trayDetailRecordMapper.batchUpdateTrayDetailRecord(trayDetailRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除托盘明细纪录
     *
     * @param ids 需要删除的托盘明细纪录主键集合
     * @return 结果
     */
    @Override
    public int deleteTrayDetailRecordByIds(Long[] ids) {
        return trayDetailRecordMapper.deleteTrayDetailRecordByIds(ids);
    }

    /**
     * 删除托盘明细纪录信息
     *
     * @param id 托盘明细纪录主键
     * @return 结果
     */
    @Override
    public int deleteTrayDetailRecordById(Long id) {
        return trayDetailRecordMapper.deleteTrayDetailRecordById(id);
    }

    @Override
    public TrayDetailRecord selectTrayDetailRecordByNo(String no) {
        return trayDetailRecordMapper.selectTrayDetailRecordByNo(no);
    }
}
